package com.xiyoo.cw.filter;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * @Description 请求流水过滤器
 * @Author zhouzhongqing
 * @Date 2024/6/18 11:23
 * @Version 1.0
 **/
@Slf4j
@Order(0)
@Component
public class ThreadLogFilter implements Filter {

    private static final String UNIQUE_ID_NAME = "traceId";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        MDC.put(UNIQUE_ID_NAME, IdUtil.simpleUUID());
        try {
            log.info("请求来了");
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            MDC.remove(UNIQUE_ID_NAME);
        }
    }

    @Override
    public void destroy() {
    }
}
